import 'dart:ui';
import 'dart:convert' as convert;
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
// import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:graduation_project/cartoon_module/cartoon_list_double.dart';
import 'package:graduation_project/common/global.dart';
import 'package:graduation_project/common/screen_body.dart';
import 'package:graduation_project/leader/leader_mid.dart';
import 'package:graduation_project/menu_bar/search/search_bar.dart';
// import 'leader_module/cartoon_list_double.dart';
// import 'leader_module/leader_mid.dart';
// ignore: non_constant_identifier_names
// import 'package:graduation_project/leader_module/cartoon_list_double.dart';

// 为了保证滑动的时候多个页面进行交互，可以新建一个公共类里面，例如tool工具类
// 检测到监听进行透明度值的计算，或者说可以改变rgba的数值，当前数值（需要使用主体图片颜色）到 255 255 255 1
// ignore: camel_case_types
class leader_menu extends StatefulWidget {
  // const leader_menu({Key? key}) : super(key: key);

  @override
  State<leader_menu> createState() => _leader_menuState();
}

List colorL = [Colors.red, Colors.blue, Colors.white];
int i = 0;

class _leader_menuState extends State<leader_menu>
    with SingleTickerProviderStateMixin {
  // Color SwColor;
  List<String> imageList = [
    "http://www.itying.com/images/flutter/1.png",
    "http://www.itying.com/images/flutter/2.png",
    "http://www.itying.com/images/flutter/3.png"
  ];
  List imageListC = [
    {
      'imageUrl': 'http://www.itying.com/images/flutter/1.png',
      'imagecolor': Colors.transparent
    },
  ];
// 颜色渐变动画

  AnimationController controller0_1;
  Animation<Color> color0_1;

  AnimationController controller;
  Animation animation;
  List SwReColor;

  int _titleAlpha = 0; //范围 0-255
  ScrollController _scrollController = new ScrollController();
  Future _future;
  List car_listRes = [];
  int dou_page = 0;
  _getinformation() async {
    dou_page++;
    Response response;
    var dio = Dio();
    dio.options.responseType = ResponseType.plain;
    response =
        await dio.get('http://119.91.231.198:3033/getCarTage?tage_id=11');
    var res = convert.jsonDecode(response.toString());
    for (var i = 6 * (dou_page - 1); i < 6 * dou_page; i++) {
      // print(res['contentArr']);
      car_listRes.add(res['contentArr'][i]);
    }
    // print(res['contentArr'][0]);
  }

  @override
  void initState() {
    print('进入0首页');
    super.initState();
    _future = _getinformation();
    controller = AnimationController(
        duration: Duration(seconds: 2), vsync: this); //AnimationController

    animation = ColorTween(
            begin: Color.fromRGBO(80, 200, 255, 1),
            end: Color.fromRGBO(145, 232, 239, 1))
        .animate(controller);
    // controller.repeat(reverse: true);
    // 切换页面会导致报错 暂时关闭，先做其他
    print('');
    controller.addListener(() {
      setState(() {});
    });
    // 下拉加载
    // _scrollController = new ScrollController();
    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        // _onLoadmore();
        print('滑到了最底部');
        _getinformation();
        // cartoon_list0..addAll(cartoon_list1);
        // print(cartoon_list0.length);
        setState(() {});
      }
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    // controller.stop();
    // 离开时关闭
    _scrollController.dispose();
    print('离开0首页');
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // double appbar0_opacity = 0;
    return Scaffold(
      appBar: AppBar(
        // backgroundColor: Color.fromRGBO(255, 250, 250, 0),
        backgroundColor: Colors.white.withAlpha(_titleAlpha),
        elevation: 0,
        flexibleSpace: Container(
          alignment: Alignment.center,
          child: Row(children: [
            Container(
                // width: MediaQuery.of(context).size.width,
                width: 340,
                margin: EdgeInsets.only(top: 40, left: 14, right: 0),
                padding: EdgeInsets.all(4),
                decoration: new BoxDecoration(
                  color: Colors.black12,
                  // 设置四周圆角，角度
                  borderRadius: BorderRadius.all(Radius.circular(10.0)),
                ),
                child: InkWell(
                  onTap: () => {
                    // 跳转到搜索页面
                    // print('111'),
                    // print(appbar0_opacity),

                    Navigator.push(context, MaterialPageRoute(builder: (_) {
                      return search_bar();
                    })),
                  },
                  child: Row(
                    children: [
                      Container(
                        margin: EdgeInsets.all(4),
                        child: Icon(
                          Icons.search,
                          color: Colors.black38,
                          size: 15,
                        ),
                      ),
                      Text('默认样式',
                          style:
                              TextStyle(color: Colors.black38, fontSize: 12)),
                    ],
                  ),
                )),
            InkWell(
              onTap: () {
                print('分类');

                Navigator.push(context, MaterialPageRoute(builder: (_) {
                  return Scaffold(
                    // extendBodyBehindAppBar: true,
                    // extendBody: true,
                    appBar: AppBar(
                      iconTheme: IconThemeData(
                        color: Colors.black87, //修改颜色
                      ),
                      centerTitle: true,
                      backgroundColor: Color.fromRGBO(255, 255, 255, 1),
                      // leading:
                      title: Text(
                        '分类',
                        style: TextStyle(color: Colors.black87),
                      ),
                    ),
                    body: Stack(
                      children: [
                        // content(),
                        // bottomBar(),
                        screen_body(),
                      ],
                    ),
                  );
                }));
              },
              child: Container(
                margin: EdgeInsets.only(top: 40, left: 8, right: 8),
                padding: EdgeInsets.only(top: 4, bottom: 4, left: 8, right: 8),
                decoration: new BoxDecoration(
                  color: Colors.black12,
                  // 设置四周圆角，角度
                  borderRadius: BorderRadius.all(Radius.circular(10.0)),
                  // 设置四周边框
                  // border: new Border.all(),
                ),
                child: Row(
                  children: [
                    Container(
                      margin: EdgeInsets.only(left: 4, top: 4, bottom: 4),
                      child: Icon(
                        Icons.format_list_bulleted,
                        color: Colors.black38,
                        size: 15,
                      ),
                    ),
                    Text('分类',
                        style: TextStyle(color: Colors.black38, fontSize: 12))
                  ],
                ),
              ),
            ),
            // Opacity(opacity: appbar0_opacity)
          ]),
        ),
      ),
      body: FutureBuilder(
        future: _future,
        builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            return Container(
              // color: Color.fromRGBO(30, 138, 223, 0.7),
              // color: SwColor,
              // RefreshIndicator 自带的下拉刷新
              child: RefreshIndicator(
                onRefresh: () async {
                  //模拟网络请求
                  print('上拉刷新');
                  // print(cartoon_list0);
                  await Future.delayed(Duration(milliseconds: 2000));
                  //结束刷新
                  return Future.value(true);
                },
                child: ListView(
                    physics: const BouncingScrollPhysics(),
                    controller: _scrollController,
                    // shrinkWrap: true,
                    children: [
                      Container(
                        margin: EdgeInsets.only(top: 00, bottom: 0),
                        padding: EdgeInsets.only(top: 10, bottom: 2),
                        height: 200,
                        // color: colorL[i],
                        // color: animation.value,
                        child: new Swiper(
                          itemBuilder: (BuildContext context, int index) {
                            return new Image.network(
                              imageList[index],
                              fit: BoxFit.cover,
                            );
                          },
                          onIndexChanged: (index) {
                            i = index;
                          },
                          itemCount: imageList.length,
                          viewportFraction: 0.9,
                          scale: 0.9,
                          loop: true,
                          autoplay: true,
                          duration: 1000,
                          autoplayDelay: 5000,
                        ),
                      ),
                      leader_mid(),
                      Container(
                        alignment: Alignment.center,
                        child: cartoon_list_double(
                          cartoon_list: car_listRes,
                        ),
                      ),
                      // Container(
                      //   height: 600,
                      // ),
                    ]),
                // )
              ),
            );
          } else {
            return Container();
          }
        },
      ),
    );
  }
}
